import APIConfig from "../config/api"
import exceptionMessage from "../config/exception-messge";
import wxToPromise from "./wx";


class Http {
  static async request ({url, data, method = 'GET'}) {

    const res = await wxToPromise('request', {
      url: APIConfig.baseUrl + url,
      data,
      method
    })

    if (res.statusCode < 400) {
      return res.data.data
    }
    // 请求失败
    if (res.statusCode === 401) {
      // 令牌相关操作
      return
    }
    Http._showError(res.data.error_code, res.data.message)
  }

  static _showError (error_code, message) {
    let title = ''
    const errorMessage = exceptionMessage[error_code]
    title = errorMessage || message || '未知异常'

    title = typeof title === 'object' ? Object.values(title).join(';') : title

    wx.showToast({
      title,
      icon: 'none',
      duration: 3000
    })
    console.log(errorMessage);
  }
}

export default Http